package cn.ainit.yz.execut;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.org.apache.bcel.internal.generic.RET;

import cn.ainit.common.ReturnObject;
import cn.ainit.sys.ds.store.StoreService;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.utils.UUID32;
import cn.ainit.yz.execut.entity.Execut;
import cn.ainit.yz.execut.entity.ExecutFM;
import cn.ainit.yz.execut.entity.ExecutZD;
import cn.ainit.yz.execut.entity.ExecutZW;
import cn.ainit.yz.printexplain.entity.PrintExplainFM;
import cn.ainit.yz.printexplain.entity.PrintExplainZD;
import cn.ainit.yz.printexplain.entity.PrintExplainZW;

@Controller
@RequestMapping("/yzexecut")
public class ExecutController {
	private ExecutService executService;
	@Resource
	public void setExecutService(ExecutService executService) {
		this.executService = executService;
	}
	
	private StoreService storeService;

	@Resource
	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}
	
	/**
	 * 保存修改综合施工单
	 * @param execut
	 * @param request
	 * @return
	 * @author wyang
	 * @date : 2014-2-28 下午01:27:19
	 */
	@RequestMapping("/saveExBase")
	@ResponseBody
	public ReturnObject saveExBase(Execut execut, HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			if (execut.getExId() != null && (!execut.getExId().equals(""))) {
				//有主键   说明是更新
				if(executService.updateExBase(execut)){
					returnObject.setMsg("upsuccess");
				}else {
					returnObject.setMsg("upfail");
				}
			}else{
				//没有主键  保存
				String peIdStr = UUID32.newGuid();
				execut.setExId(peIdStr);//设置主键
				if (executService.saveExBase(execut)) {
					returnObject.setMsg("success");
					returnObject.setInfo(peIdStr);
				}else {
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnObject.setMsg(e.getMessage());
		}
		return returnObject;
	}
	/**
	 * 保存封面印刷
	 * @param printExplainFMs
	 * @param peId
	 * @param request
	 * @return
	 * @author wyang
	 * @date : 2014-2-28 下午03:13:34
	 */
	@RequestMapping("/saveExFM")
	@ResponseBody
	public ReturnObject saveExFM(@RequestBody List<ExecutFM> executFMs,String peId,HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			List<String> ids = executService.saveExFM(executFMs,peId);
			if(ids != null && ids.size() == executFMs.size()){
				returnObject.setMsg("success");
				returnObject.setObjList(ids);
			}else {
				returnObject.setMsg("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnObject.setMsg(e.getMessage());
		}
		return returnObject;
	}
	/**
	 * 保存正文印刷
	 * @param executZWs
	 * @param peId
	 * @param request
	 * @return
	 * @author wyang
	 * @date : 2014-3-3 上午10:23:42
	 */
	@RequestMapping("/saveExZW")
	@ResponseBody
	public ReturnObject saveExZW(@RequestBody List<ExecutZW> executZWs,String peId,HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			List<String> ids = executService.saveExZW(executZWs, peId);
			if(ids != null && ids.size() == executZWs.size()){
				returnObject.setMsg("success");
				returnObject.setObjList(ids);
			}else {
				returnObject.setMsg("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnObject.setMsg(e.getMessage());
		}
		return returnObject;
	}
	/**
	 * 保存装订项目
	 * @param executZDs
	 * @param peId
	 * @param request
	 * @return
	 * @author wyang
	 * @date : 2014-3-3 上午10:25:05
	 */
	@RequestMapping("/saveExZD")
	@ResponseBody
	public ReturnObject saveExZD(@RequestBody List<ExecutZD> executZDs,String peId,HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			List<String> ids = executService.saveExZD(executZDs,peId);
			if(ids != null && ids.size() == executZDs.size()){
				returnObject.setMsg("success");
				returnObject.setObjList(ids);
			}else {
				returnObject.setMsg("fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
			returnObject.setMsg(e.getMessage());
		}
		return returnObject;
	}
	
	/**
	 * 加载所有的明细列表
	 * @param ysId
	 * @param request
	 * @return
	 * @author wyang
	 * @date : 2014-3-3 下午01:39:59
	 */
	@RequestMapping("/findItems")
	@ResponseBody
	public ReturnObject findItems(String ysId, HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		try {
			List<ExecutFM> printExplainFMs = this.executService.findFMList(ysId);
			List<ExecutZW> printExplainZWs = this.executService.findZWList(ysId);
			List<ExecutZD> printExplainZDs = this.executService.findZDList(ysId);
			List list = new ArrayList();
			list.add(printExplainFMs);
			list.add(printExplainZWs);
			list.add(printExplainZDs);
			returnObject.setObjList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 综合施工单生成印厂入库单
	 * @param exId
	 * @param oId
	 * @param dsId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 */
	@RequestMapping("/executToSprk")
	@ResponseBody
	public ReturnObject newSprk(String exId,String oId,String dsId,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			//执行存储过程
			String spId = this.executService.exeToSprk(exId,oId);
			returnObject.setInfo("SPRK_ID-"+spId);
			String[] dsIds = dsId.split(";"); 
			DataStore dataStore = storeService.findStore(dsIds[0]);
			returnObject.setDynObj(dataStore);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject ;
	}
	
	
	
	
	
	
}
